#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
    bool isAlienSorted(vector<string> &words, string order)
    {
        // 记录每个字母的order
        vector<int> order_vt(26, 0);
        for (int i = 0; i < order.size(); i++)
        {
            order_vt[order[i] - 'a'] = i;
        }
        // 查表判断是否合法
        for (int i = 1; i < words.size(); i++)
        {
            string &s1 = words[i - 1];
            string &s2 = words[i];
            int j = 0;
            bool legal = false;
            while (j < s1.size() && j < s2.size())
            {
                int j1_order = order_vt[s1[j] - 'a'];
                int j2_order = order_vt[s2[j] - 'a'];
                j++;
                if (j1_order < j2_order)
                {
                    legal = true;
                    break;
                }
                if (j1_order > j2_order)
                    return false;
            }
            if (legal)
                continue;
            if (j < s1.size() && j == s2.size())
                return false;
        }
        return true;
    }
};